package com.ndp.fb.mq.service.schedulerservice.business;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ndp.fb.fbagent.service.AdSetFBService;
import com.ndp.fb.model.fbagent.param.adset.AdSetParam;
import com.ndp.fb.mq.api.service.schedulerservice.business.AdSetUpdateService;
import com.ndp.fb.rdb.api.AdSetRdbService;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jacky.cheng on 2015/7/18.
 */
@Component
public class AdSetUpdateServiceImpl implements AdSetUpdateService {
    org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    @Reference
    private AdSetRdbService adSetRdbService;
    @Reference
    private AdSetFBService adSetFBService;

    @Override
    public void updateBidInfo(Map<String, String> equipment, Object message) {
        AdSetParam adSetParam= (AdSetParam) message;
        logger.info("delay update adSet bidAmount.... adSetId:{},bidAmount:{}", adSetParam.getCampaignId(),adSetParam.getBidAmount());
        logger.info(JSON.toJSONString(adSetParam));
        List<Map<String, Object>> fieldMap = new ArrayList<>();
        Map<String, Object> fields = new HashMap<>();
        fields.put("adSetId", adSetParam.getCampaignId());
        fields.put("bidInfo", adSetParam.getBidInfo());
        fields.put("bid_amount", adSetParam.getBidAmount());
        fieldMap.add(fields);
        adSetParam.setBidInfo(null); //readonly
        adSetParam.setDailyBudget(null);
        adSetParam.setLifetimeBudget(null);
        adSetParam.setStatus(null);
        adSetFBService.updateAdSet(adSetParam);
        adSetRdbService.updateAdSetsByAdSetIds(fieldMap);
    }
}
